package com.qf.java2201.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.java2201.common.PageVo;
import com.qf.java2201.mapper.ProductMapper;
import com.qf.java2201.pojo.Goods;
import com.qf.java2201.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVo<Goods> getPageData(Map<String, Object> params) {
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        //mybatis分页插件
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<Goods> goods = productMapper.selectByParams(params);

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        //把分页的数据封装到pageVO
        PageVo<Goods> pageVo = new PageVo<>();
        pageVo.setCode(0);
        pageVo.setMsg("商品列表数据");
        pageVo.setData(goodsPageInfo.getList());
        pageVo.setCount(goodsPageInfo.getTotal());
        return pageVo;
    }

    @Override
    public int updateProductById(Goods goods) {
        return productMapper.updateProductById(goods);
    }

    @Override
    public int deleteProductById(Goods goods) {
        return productMapper.deleteProductById(goods);
    }

    @Override
    public int addProduct(Goods goods) {
        return productMapper.addProduct(goods);
    }

    @Override
    public List<Goods> selectList(Goods goods) {
        return productMapper.selectList(goods);
    }

    @Override
    public void addProducts(List<Goods> goods) {
        productMapper.addProducts(goods);
    }
}
